<?php
namespace common\models;

use Yii;
use services\models\Goods;


use services\models\CommentsRelation;


class GoodsListRedis
{
    /**
     * 获取商品列表及分页数组
     * @param 当前页数 $page
     * @param 当前参数 $request
     * return array
     */
    public static function getListhtml($request) {
        //一页显示数
        $count = 24;
    
        //获取商品的IDS
        $id_arr = self::actionSearch($request);
        $arr_count = $id_arr['total_num'];
        $search_back = $id_arr['brand_list'];
        $id_arr = $id_arr['goods_list'];
        //获取商品列表
        $goods_info = [];
        foreach ($id_arr as $id_key=>$id_val) {
            //获取商品信息
            $goods_list = [];
            if(empty($id_val)) {
                continue;
            }
            $goods_list = GoodsRedis::getListRedis($id_val);
            if (empty($goods_list)) {
                continue;
            }
            $goods_info[$id_key]['goods_id'] = $goods_list['goods_id'];
            $goods_info[$id_key]['goods_name'] = $goods_list['goods_name']; //商品名称
            $goods_info[$id_key]['defaultsku'] = $goods_list['defaultsku']; //默认SKUid
            if(isset($goods_list['image']['big'][0])) {
                $goods_info[$id_key]['image'] = $goods_list['image']['big'][0]; //商品图片
            } else {
                $goods_info[$id_key]['image'] = '';
            }
    
            //获取第一件商品的价格
            $products = GoodsRedis::getSkuRedis($goods_info[$id_key]['defaultsku']);
            $goods_info[$id_key]['sale_price'] = $products['sale_price'];
            $goods_info[$id_key]['try_price'] = $products['try_price'];
            $goods_info[$id_key]['type'] = $products['type'];
            $goods_info[$id_key]['image'] = Goods::findByGoodsIdFromRedis($id_val, ['pic', 'bigpic']); //商品图片
            //评论数
            $CommentsModel = new CommentsRelation();
            $reply_count = $CommentsModel->getFeedbackTotal($goods_list['goods_id']);
            $goods_info[$id_key]['count'] = !empty($reply_count)?$reply_count:0;
        }
        $arr = array('text'=>$goods_info);
        $return_arr['arr'] = $arr;
        $return_arr['arr_count'] = $arr_count;
        $return_arr['count'] = $count;
        $return_arr['search_back'] = $search_back;
        return $return_arr;
    }
    
    /**
     * 获取搜索信息
     * return array()
     */
    public static function actionSearch($request) {
        $goods_name = !empty($request['goods_name'])?urldecode(trim($request['goods_name'])):''; // 商品名称
        $brand_id = !empty($request['brand_id'])?$request['brand_id']:0; // 品牌
        $price_start = !empty($request['price_start'])?$request['price_start']:-1; // 起始价格区间
        $price_end = !empty($request['price_end'])?$request['price_end']:-1; // 结束价格区间
        $try_price_start = !empty($request['try_price_start'])?$request['try_price_start']:-1; // 起始试豆区间
        $try_price_end = !empty($request['try_price_end'])?$request['try_price_end']:-1; // 结束试豆区间
        $type = !empty($request['type'])?$request['type']:0; // 类型
        $stock = !empty($request['stock'])?$request['stock']:0; // 仅显示有库存 0无限制
        $sort_type = !empty($request['sort_type'])?$request['sort_type']:0; // 排序类型
        $sort = !empty($request['sort'])?$_REQUEST['sort']:'DESC'; // 排序方式 ASC DESC
        $page_num = !empty($request['page'])?$request['page']:'1';
        $category = !empty($request['category'])?$request['category']:'1';
    
        $list_ids = (new \services\models\SolrService())->search(
            $category,$goods_name, $brand_id, $price_start, $price_end, $try_price_start, $try_price_end, $type, $stock, $sort_type, $sort, $page_num
        );
        return $list_ids;
    }
    
    /**
     * 获取品牌Qindex
     * return array
     */
    public static function getQIndex($search_back) {
        //搜索栏 类别
        foreach ($search_back as $k=>$v) {
            $qindex[] = $k;
        }
        return $qindex;
    }
}